package com.admin.system.controller;

import cn.hutool.core.util.StrUtil;
import com.admin.model.base.PageResult;
import com.admin.model.pojo.User;
import com.admin.model.dto.UserDto;
import com.admin.system.service.UserService;
import com.admin.system.service.mapstruct.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.groups.Default;
import java.util.*;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2021-01-31 19:19:30
 */
@Api(tags = "用户")
@Validated
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    private UserService userService;


    @ApiOperation("部门id查询用户")
    @PreAuthorize("hasAnyAuthority('user:list')")
    @GetMapping("/dept/{deptId}")
    public ResponseEntity<Object> queryByDeptId(@PathVariable Long deptId,
                                                @RequestParam(defaultValue = "1") int pageNum,
                                                @RequestParam(defaultValue = "10") int pageSize,
                                                @RequestParam(defaultValue = "-id") String orderBy) {
        PageResult pageResult = userService.queryByDeptId(deptId, pageNum, pageSize, orderBy);
        return ResponseEntity.ok(pageResult);
    }

    @ApiOperation("重置用户密码")
    @PreAuthorize("hasAnyAuthority('user:edit')")
    @PutMapping("/reset/{id}")
    public ResponseEntity<Object> resetPassword(@PathVariable Long id) {
        userService.resetPasswordById(id);
        return ResponseEntity.ok("重置密码成功，新密码为“123456”");
    }

    @ApiOperation("授权角色")
    @PreAuthorize("hasAnyAuthority('user:add')")
    @PostMapping("/auth")
    public ResponseEntity<String> authRoles(@NotEmpty(message = "角色id不能为空") @RequestBody List<Long> roleIds,
                                            @NotEmpty(message = "用户id不能为空") @RequestParam Long userId) {
        userService.authRoles(userId, roleIds);
        return ResponseEntity.ok("授权成功");
    }

    @ApiOperation("授权部门")
    @PreAuthorize("hasAnyAuthority('user:add')")
    @PostMapping("/dept")
    public ResponseEntity<String> authDepts(@NotEmpty(message = "部门id不能为空") @RequestBody List<Long> deptIds,
                                            @NotEmpty(message = "用户id不能为空") @RequestParam Long userId) {
        userService.authDepts(userId, deptIds);
        return ResponseEntity.ok("授权成功");
    }

    @ApiOperation("授权岗位")
    @PreAuthorize("hasAnyAuthority('user:add')")
    @PostMapping("/job")
    public ResponseEntity<String> authJobs(@NotEmpty(message = "岗位id不能为空") @RequestBody List<Long> jobIds,
                                           @NotEmpty(message = "用户id不能为空") @RequestParam Long userId) {
        userService.authJobs(userId, jobIds);
        return ResponseEntity.ok("授权成功");
    }


    @ApiOperation("id查询")
    @PreAuthorize("hasAnyAuthority('user:list')")
    @GetMapping("/{id}")
    public ResponseEntity<UserDto> findById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(userService.queryById(id));
    }

    @ApiOperation("条件查询")
    @ApiOperationSupport(ignoreParameters = {"createdUser", "updatedUser", "roles", "depts", "jobs", "roleIds", "deptIds", "jobIds"})
    @PreAuthorize("hasAnyAuthority('user:list')")
    @PostMapping("/search")
    public ResponseEntity<Object> findAll(@RequestParam(defaultValue = "1") int pageNum,
                                          @RequestParam(defaultValue = "10") int pageSize,
                                          @RequestParam(defaultValue = "-id") String orderBy,
                                          @RequestBody(required = false) UserDto userDto) {
        PageResult pageResult = userService.queryAll(userDto, pageNum, pageSize, orderBy);
        return ResponseEntity.ok(pageResult);
    }


    @ApiOperation("ids删除")
    @PreAuthorize("hasAnyAuthority('user:del')")
    @DeleteMapping
    public ResponseEntity<String> deleteByIds(@RequestBody Set<Long> ids) {
        userService.deleteByIds(ids);
        return ResponseEntity.ok("删除成功");
    }

    @ApiOperation("创建")
    @ApiOperationSupport(ignoreParameters = {"id", "createTime", "updateTime", "createdBy", "updatedBy", "createdUser", "updatedUser", "roles", "depts", "jobs"})
    @PreAuthorize("hasAnyAuthority('user:add')")
    @PostMapping
    public ResponseEntity<String> create(@RequestBody @Validated({UserDto.Create.class, Default.class}) UserDto userDto) {
        userService.create(userDto);
        return ResponseEntity.ok("创建成功");
    }

    @ApiOperation("id更新")
    @ApiOperationSupport(ignoreParameters = {"createTime", "updateTime", "createdBy", "updatedBy", "roles", "depts", "jobs", "createdUser", "updatedUser"})
    @PreAuthorize("hasAnyAuthority('user:edit')")
    @PutMapping
    public ResponseEntity<String> updateById(@RequestBody @Validated({UserDto.Update.class, Default.class}) UserDto userDto) {
        userService.updateById(userDto);
        return ResponseEntity.ok("修改成功");
    }
}